<?php

/**
 * blogs actions.
 *
 * @package    IPLIFE
 * @subpackage blogs
 * @author     alexey.shapran@gmail.com
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class blogsActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function preExecute()
	{
		$this->setLayout('bloglayout');
	}
	public function executeIndex(sfWebRequest $request)
	{
		$con = Doctrine_Manager::getInstance()->connection();

		$query = " SELECT a.*, u.lastname, u.firstname, u.image FROM ( SELECT * FROM blog_post bp ORDER BY bp.updated_at DESC ) a LEFT JOIN sf_guard_user u ON a.user_id  = u.id  GROUP BY user_id ORDER BY a.updated_at DESC LIMIT 20 ";
		$st = $con->execute($query);
		$this->last20blogs = $st->fetchAll();
		$this->last5posts = array();		
		foreach($this->last20blogs as $post)
		{
			$this->last5posts[$post['user_id']] = Doctrine_Query::create()
															->select()
															->from('BlogPost')
															->orderBy('updated_at DESC')
															->limit('5')
															->where('id != ?', $post['id'])
															->andWhere('user_id = ?',$post['user_id'])
															->execute();
		}
	}
	public function executeAuthor(sfWebRequest $request)
	{
		$numberofblogsperauthorperpage = 20;
		$this->author = Doctrine::getTable('sfGuardUser')->findOneBy('id',$request->getParameter('id')) ;
		$q = Doctrine_Query::create()
		->from('BlogPost')
		->where('user_id = ?', $this->author->getId())
		->orderBy('updated_at DESC');
		
		$this->posts = new sfDoctrinePager('BlogPosts', $numberofblogsperauthorperpage);
		$this->posts->setQuery($q);
		$this->posts->setPage($request->getParameter('pnumber'),1);
		$this->posts->init();
	}
	public function executeAdd(sfWebRequest $request)
	{
		$this->form = new BlogPostForm();
	}
	public function executePost(sfWebRequest $request)
	{
		$this->form = new BlogPostForm();
		$this->processForm($request, $this->form);
		$this->setTemplate('add');
	}
	public function executeView(sfWebRequest $request)
	{
		$this->post = $this->getRoute()->getObject();
		$this->author = Doctrine::getTable('sfGuardUser')->findOneBy('id',$this->post->getUserId());
		$response = $this->getResponse();
		$response->addMeta('keywords', $this->post['tags']);
		$response->addMeta('title', 'IPLIFE | Блоги | '.$this->post['title']);
		$response->addMeta('description', $this->post['short']);
	}
	public function executeEdit(sfWebRequest $request)
	{
		$blogpost =  $this->getRoute()->getObject();
		$this->form = new BlogPostForm($blogpost);
	}
	public function executeSave(sfWebRequest $request)
	{
		$thisid = $request->getParameter('post');
		$thispost = Doctrine::getTable('BlogPost')->findBy('id',$thisid['id']);
		$this->form = new BlogPostForm($thispost[0]);
		$this->processForm($request, $this->form);
		$this->setTemplate('edit');
	}

	protected function processForm(sfWebRequest $request, sfForm $form, $url='blogs_author')
	{
		$post = $request->getParameter('post');
		$form->bind(array(
				  'id'					=> $post['id'],
			      'title'               => $post['title'],
			      'short'               => $post['short'],
			      'body'                => $post['body'],
			      'tags'                => $post['tags'],
			      'user_id'             => $this->getUser()->getGuardUser()->getId(),
			      'is_allowed_comments' => true,
			      'updated_at'          => time(),
			  	  '_csrf_token'			=> $post['_csrf_token']
		));

		if($form->isValid())
		{
			$form->save();
			$this->redirect($url, array('id'=> $this->getUser()->getGuardUser()->getId(), 'pnumber'=>'1'));
		}
		else
		return true;
	}
}
